Skip to content

Fix mypy#568

Merged
joamatab merged 1 commit intomainfrom
mypy
Apr 12, 2025
Merged

Fix mypy#568
joamatab merged 1 commit intomainfrom
mypy

Conversation

@MatthewMckee4
Copy link
Copy Markdown
Contributor

@MatthewMckee4 MatthewMckee4 commented Apr 12, 2025

Summary by Sourcery

Add type hints and improve type safety across multiple files in the gplugins project

Enhancements:

  • Systematically added type hints to function signatures
  • Improved type safety by using more specific type annotations
  • Removed unused imports and cleaned up type-related code

CI:

  • Added mypy type checking hook to pre-commit configuration

Chores:

  • Updated pre-commit configuration to include mypy type checking
  • Simplified some function implementations to work with type checking

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Apr 12, 2025

Reviewer's Guide by Sourcery

This pull request focuses on improving code quality and maintainability by adding type hints and removing unused code. It also fixes a potential error in klayout/get_netlist.py and updates the pre-commit configuration to include mypy and remove unnecessary dependencies.

Updated class diagram for NetlistSpiceReaderDelegate

classDiagram
    class NetlistSpiceReaderDelegate {
        +wants_subcircuit(name: str) bool
        +element(
          element: str,
          value: Any,
          nets: Sequence[kdb.Net],
          parameters: dict[str, int | float | str],
        ) bool
    }
    class CalibreSpiceReader {
        +wants_subcircuit(name: str) bool
        +element(
          element: str,
          value: Any,
          nets: Sequence[kdb.Net],
          parameters: dict[str, int | float | str],
        ) bool
    }
    NetlistSpiceReaderDelegate <|-- CalibreSpiceReader
Loading

Updated class diagram for GdsfactorySpiceReader

classDiagram
    class GdsfactorySpiceReader {
        +wants_subcircuit(name: str) bool
    }
    class CalibreSpiceReader {
        +wants_subcircuit(name: str) bool
    }
    CalibreSpiceReader <|-- GdsfactorySpiceReader
Loading

File-Level Changes

Change Details Files
Added type hints and annotations for improved code clarity and maintainability.
  • Added type hints to function signatures.
  • Added type annotations for variables.
  • Used dict[str, Any] for generic dictionaries.
  • Used npt.NDArray for numpy arrays.
  • Used Iterator[int] for iterators.
  • Used Sequence[float] for sequences of floats.
gplugins/gmsh/xyz_mesh.py
gplugins/common/utils/plot.py
gplugins/vlsir/export_netlist.py
gplugins/common/utils/get_effective_indices.py
gplugins/gmsh/define_polysurfaces.py
gplugins/klayout/tests/test_plot_nets.py
gplugins/common/utils/get_component_with_net_layers.py
gplugins/common/utils/parse_layer_stack.py
gplugins/gmsh/uz_xsection_mesh.py
gplugins/klayout/netlist_spice_reader.py
gplugins/klayout/get_netlist.py
gplugins/klayout/netlist_graph.py
gplugins/klayout/plot_nets.py
Removed unused code.
  • Removed conditional block at the end of gplugins/common/utils/get_component_with_net_layers.py.
  • Removed conditional block at the end of gplugins/common/utils/plot.py.
gplugins/common/utils/plot.py
gplugins/common/utils/get_component_with_net_layers.py
Modified klayout/get_netlist.py to ensure layers is not None.
  • Added an assertion to check if layers is not None.
gplugins/klayout/get_netlist.py
Updated pre-commit configuration to remove devsim and schematic from install dependencies.
  • Removed devsim and schematic from the list of extra install dependencies in .pre-commit-config.yaml.
.pre-commit-config.yaml
Added mypy pre-commit hook.
  • Added mypy pre-commit hook to .pre-commit-config.yaml.
  • Added gdsfactory and pytest as additional dependencies for mypy.
.pre-commit-config.yaml

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions github-actions Bot added the bug Something isn't working label Apr 12, 2025
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've reviewed this pull request using the Sourcery rules engine. If you would also like our AI-powered code review then let us know.

Comment thread gplugins/gmsh/xyz_mesh.py
@@ -137,9 +142,9 @@ def define_prisms(
def xyz_mesh(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (code-quality): Low code quality found in xyz_mesh - 24% (low-code-quality)


ExplanationThe quality score for this function is below the quality threshold of 25%.
This score is a combination of the method length, cognitive complexity and working memory.

How can you solve this?

It might be worth refactoring this function to make it shorter and more readable.

  • Reduce the function length by extracting pieces of functionality out into
    their own functions. This is the most important thing you can do - ideally a
    function should be less than 10 lines.
  • Reduce nesting, perhaps by introducing guard clauses to return early.
  • Ensure that variables are tightly scoped, so that code using related concepts
    sits together within the function rather than being scattered.

Copy link
Copy Markdown
Contributor

@joamatab joamatab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you Matthew!

@joamatab joamatab merged commit 7197051 into main Apr 12, 2025
5 of 16 checks passed
@joamatab joamatab deleted the mypy branch April 12, 2025 17:22
@sourcery-ai sourcery-ai Bot mentioned this pull request May 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants